package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.common.cfg.GuavaCache;
import com.example.service.AsyncTaskService;
import com.example.vo.Cat;
import com.example.vo.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/***
 * @Description:多线程异步任务
 * @Author: Mr.Zhang
 * @Date: 2023/5/9
 */
@Api(tags = "多线程异步任务")
@RestController
public class CompletableFutureController {

    @Resource
    GuavaCache guavaCache;

    // 创建线程安全的Map，模拟cat信息的存储
    static Map<String, Cat> hashMap = Collections.synchronizedMap(new HashMap<String, Cat>());

    static {
        hashMap.put("1", new Cat("qiuqiu", 3, "吃冻干"));
        hashMap.put("2", new Cat("bayue", 2, "吃猫条"));
    }

    @Autowired
    private AsyncTaskService asyncTaskService;

    @ApiOperation(value = "同步模拟http请求")
    @GetMapping("block")
    public ResultData block() {
        List<Object> mergedList = null;
        long time = 0;
        try {
            long start = System.currentTimeMillis();
            //同步模拟http请求
            CompletableFuture<String> task1 = asyncTaskService.doTaskBlock(guavaCache.get("url"), "1");
            CompletableFuture<String> task2 = asyncTaskService.doTaskBlock(guavaCache.get("url"), "2");
            //等待所有任务完成
            CompletableFuture.allOf(task1, task2).join();
            //合并数据
            JSONObject obj1 = JSONObject.parseObject(task1.get());
            JSONObject obj2 = JSONObject.parseObject(task2.get());
            List<Object> data = Arrays.asList(obj1.get("data"));
            List<Object> data1 = Arrays.asList(obj2.get("data"));
            mergedList = new ArrayList<>();
            mergedList.addAll(data);
            mergedList.addAll(data1);
            long end = System.currentTimeMillis();
            time = end - start;
            System.out.println("耗时" + TimeUnit.MILLISECONDS.toSeconds(time) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultData.success(mergedList, time);
    }

    @ApiOperation(value = "异步模拟http请求")
    @GetMapping("async")
    public ResultData async() {
        List<Object> mergedList = null;
        long time = 0;
        try {
            long start = System.currentTimeMillis();
            //异步模拟http请求
            CompletableFuture<String> task1 = asyncTaskService.doTaskOneAsync(guavaCache.get("url"), "1");
            CompletableFuture<String> task2 = asyncTaskService.doTaskOneAsync(guavaCache.get("url"), "2");
            //等待所有任务完成
            CompletableFuture.allOf(task1, task2).join();
            //合并数据
            JSONObject obj1 = JSONObject.parseObject(task1.get());
            JSONObject obj2 = JSONObject.parseObject(task2.get());
            List<Object> data = Arrays.asList(obj1.get("data"));
            List<Object> data1 = Arrays.asList(obj2.get("data"));
            mergedList = new ArrayList<>();
            mergedList.addAll(data);
            mergedList.addAll(data1);
            long end = System.currentTimeMillis();
            time = end - start;
            System.out.println("耗时" + TimeUnit.MILLISECONDS.toSeconds(time) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultData.success(mergedList, time);
    }


    @GetMapping("select")
    public ResultData select(String id) {
        try {
            //模拟业务耗时
            Thread.sleep(6000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultData.success(hashMap.get(id));
    }


}
